package com.scy.hospitalmanager.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;

import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Description: 跨域请求
 * @ClassName: CorsConfig
 * @Author: KuonnjiArisu
 * @Date: 2021/8/17 10:32
 * @Version: 1.0
 */
@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .maxAge(3600);
    }
//    @Bean
//    public FilterRegistrationBean corsFilter(){
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        CorsConfiguration corsConfiguration = new CorsConfiguration();
//        // 是否发送Cookie信息
//        corsConfiguration.setAllowCredentials(true);
//        // 允许任何域名
//        corsConfiguration.addAllowedOrigin(CorsConfiguration.ALL);
//        // 允许任何头部信息
//        corsConfiguration.addAllowedHeader(CorsConfiguration.ALL);
//        // 允许所有请求类型
//        corsConfiguration.addAllowedMethod(CorsConfiguration.ALL);
//        // 暴露哪些头部信息（因为跨域访问默认不能获取全部头部信息）
//        corsConfiguration.addExposedHeader("Authorization");
//        // 添加映射路径
//        source.registerCorsConfiguration("/**", corsConfiguration);
//        FilterRegistrationBean bean = new FilterRegistrationBean<>(new CorsFilter(source));
//        bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
//        return bean;
//    }

}
